package cn.wolfcode.mq;

import cn.wolfcode.service.ISeckillProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.SelectorType;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 当订单创建失败时,接收该消息,进行
 * 回补库存
 * 修改本地标识(再发送消息)
 */
@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "MQOrderFailListener",
        topic = MQConstant.ORDER_RESULT_TOPIC,
        selectorType = SelectorType.TAG,
        selectorExpression = MQConstant.ORDER_RESULT_FAIL_TAG
)
public class MQOrderFailListener implements RocketMQListener<OrderMQResult> {

    @Autowired
    private ISeckillProductService seckillProductService;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void onMessage(OrderMQResult orderMQResult) {
        log.info("开始回补库存了....");
        // 1.回补库存
        seckillProductService.asysBackRedisStockCount(orderMQResult.getSeckillId(),orderMQResult.getTime());
        // 2. 本地标识 => 发送广播消息,是所有消费者都共同操作本地标识
        rocketMQTemplate.sendOneWay(MQConstant.CANCEL_SECKILL_OVER_SIGE_TOPIC,orderMQResult.getSeckillId());
    }
}
